package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.PictureAddReq;
import com.yimils.core.goods.dto.PictureUpdateReq;
import com.yimils.core.goods.entity.PictureEntity;
import com.yimils.core.goods.mapper.PictureMapper;
import com.yimils.core.goods.serivce.PictureService;
import com.yimils.core.goods.vo.PictureRes;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("pictureService")
public class PictureServiceImpl extends ServiceImpl<PictureMapper, PictureEntity> implements PictureService {
    @Override
    public Boolean savePicture(PictureAddReq pictureAddReq) {
        PictureEntity pictureEntity = new PictureEntity();
        pictureEntity.setUrl(pictureAddReq.getUrl());
        pictureEntity.setState(Boolean.FALSE);
        pictureEntity.setCreateTime(LocalDateTime.now());
        pictureEntity.setUpdateTime(LocalDateTime.now());
        return save(pictureEntity);
    }

    @Override
    public List<PictureEntity> savePictureList(List<String> videoUrls) {
        List<PictureEntity> entities = videoUrls.stream().map(o -> {
            PictureEntity pictureEntity = new PictureEntity();
            pictureEntity.setUrl(o);
            pictureEntity.setState(Boolean.FALSE);
            pictureEntity.setCreateTime(LocalDateTime.now());
            pictureEntity.setUpdateTime(LocalDateTime.now());
            return pictureEntity;
        }).collect(Collectors.toList());
        saveBatch(entities);
        return entities;
    }

    @Override
    public Boolean updatePicture(PictureUpdateReq pictureUpdateReq) {
        QueryWrapper<PictureEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PictureEntity::getId, pictureUpdateReq.getPictureId()).eq(PictureEntity::getState, Boolean.FALSE);
        PictureEntity entity = getOne(qw);
        entity.setUrl(pictureUpdateReq.getUrl());
        entity.setUpdateTime(LocalDateTime.now());

        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deletePicture(List<Long> pictureIds) {
        QueryWrapper<PictureEntity> qw = new QueryWrapper<>();
        qw.lambda().in(PictureEntity::getId, pictureIds).eq(PictureEntity::getState, Boolean.FALSE);
        List<PictureEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        return saveOrUpdateBatch(entities);
    }

    @Override
    public List<PictureRes> listPicture() {
        QueryWrapper<PictureEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PictureEntity::getState, Boolean.FALSE);
        List<PictureEntity> entities = list(qw);
        return entities.stream().map(o -> {
            PictureRes pictureRes = new PictureRes();
            pictureRes.setPictureId(o.getId());
            pictureRes.setUrl(o.getUrl());
            return pictureRes;
        }).collect(Collectors.toList());
    }

    @Override
    public List<PictureEntity> listPicture(List<Long> pictureIds) {
        QueryWrapper<PictureEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PictureEntity::getId, pictureIds).eq(PictureEntity::getState, Boolean.FALSE);
        return list(qw);
    }
}
